package com.example.project.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.message.service.MessageService;
import com.example.project.entity.Issue;
import com.example.project.entity.IssueRelation;
import com.example.project.entity.Watcher;
import com.example.project.service.IssueRelationService;
import com.example.project.service.IssueService;
import com.example.project.service.JournalService;

/**
 * 问题关联关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-24
 */
@Service
public class IssueRelationServiceImpl extends BaseServiceImpl implements IssueRelationService {

	@Autowired
	private IssueService issueService;

	@Autowired
	private JournalService journalService;

	@Autowired
	private MessageService messageService;

	/**
	 * 查询问题关联关系
	 * 
	 * @param id 问题关联关系ID
	 * @return 问题关联关系
	 */
	@Override
	public IssueRelation selectIssueRelationById(Long id) {
		return issueRelationMapper.selectIssueRelationById(id);
	}

	/**
	 * 查询问题关联关系列表
	 * 
	 * @param issueRelation 问题关联关系
	 * @return 问题关联关系
	 */
	@Override
	public List<IssueRelation> selectIssueRelationList(IssueRelation issueRelation) {
		return issueRelationMapper.selectIssueRelationList(issueRelation);
	}

	/**
	 * 根据问题ID查询关联关系列表
	 * 
	 * @param issueId 问题ID
	 * @return 关联关系列表
	 */
	@Override
	public List<IssueRelation> selectIssueRelationsByIssueId(Long issueId) {
		// 查询issueId作为源问题或目标问题的所有关联关系
		IssueRelation issueRelation = new IssueRelation();
		issueRelation.setIssueFromId(issueId);
		List<IssueRelation> relations = issueRelationMapper.selectIssueRelationList(issueRelation);

		// 同时查询issueId作为目标问题的关联关系（双向关联）
//        IssueRelation issueRelationTo = new IssueRelation();
//        issueRelationTo.setIssueToId(issueId);
//        List<IssueRelation> relationsTo = issueRelationMapper.selectIssueRelationList(issueRelationTo);

		// 合并结果集
//        if (relationsTo != null && !relationsTo.isEmpty()) {
//            relations.addAll(relationsTo);
//        }

		return relations;
	}

	/**
	 * 删除问题关联关系信息
	 * 
	 * @param id 问题关联关系ID
	 * @return 结果
	 */
	@Override
	public int deleteIssueRelationById(IssueRelation issueRelation) {

		int result = issueRelationMapper.deleteIssueRelationById(issueRelation.getId());
		if (result > 0) {
			// 添加删除关联历史记录 - 针对源问题
			journalService.createDeleteRelationJournal(issueRelation.getIssueFromId(), issueRelation);

			// 发送消息
			Issue issue = issueService.selectIssuesById(issueRelation.getIssueFromId());
			messageService.sendIssueMessage(issue);
		}
		return result;
	}

	/**
	 * 添加问题关联
	 * 
	 * @param issueId        源问题ID
	 * @param relatedIssueId 关联问题ID
	 * @param relationType   关联类型
	 * @param description    关联描述
	 * @param createBy       创建人
	 * @return 结果
	 */
	@Override
	public int addIssueRelation(Long issueId, Long issueToId, String relationType, String description,
			String createBy) {
		Issue issue = issueService.selectIssuesById(issueToId);
		if (issue == null) {
			return -2;
		}
		// 检查是否已存在相同的关联关系
		IssueRelation existingRelation = new IssueRelation();
		existingRelation.setIssueFromId(issueId);
		existingRelation.setIssueToId(issueToId);
		List<IssueRelation> existingRelations = issueRelationMapper.selectIssueRelationList(existingRelation);

		// 如果已存在相同的关联关系，直接返回0表示跳过添加
		if (existingRelations != null && !existingRelations.isEmpty()) {
			return -1;
		}

		IssueRelation issueRelation = new IssueRelation();
		issueRelation.setIssueFromId(issueId);
		issueRelation.setIssueToId(issueToId);
		issueRelation.setRelationType(relationType);
		issueRelation.setDescription(description);
		issueRelation.setCreateBy(createBy);
		issueRelation.setCreateTime(LocalDateTime.now());
		int result = issueRelationMapper.insertIssueRelation(issueRelation);
		if (result > 0) {
			// 创建历史记录
			journalService.createRelationJournal(issueId, issueRelation);

			// 发送消息
			messageService.sendIssueMessage(issue);
		}
		return result;
	}

	@Override
	public List<IssueRelation> queryIssueRelationListByIssueId(Long issueId) {
		List<IssueRelation> relations = selectIssueRelationsByIssueId(issueId);
		if (relations != null && !relations.isEmpty()) {
			// 完善关联问题信息
			for (IssueRelation relation : relations) {
				// 根据关联关系类型确定需要显示的问题信息
				if (issueId.equals(relation.getIssueFromId())) {
					// 当前问题是源问题，显示目标问题
					Issue relatedIssue = issueService.selectIssuesById(relation.getIssueToId());
					if (relatedIssue != null) {
						relation.setIssueTo(relatedIssue);
					}
				} else if (issueId.equals(relation.getIssueToId())) {
					// 当前问题是目标问题，显示源问题
					Issue relatedIssue = issueService.selectIssuesById(relation.getIssueFromId());
					if (relatedIssue != null) {
						relation.setIssueFrom(relatedIssue);
					}
				}
			}
		}
		return relations;
	}

	@Override
	public IssueRelation findRelationByFromIdAndToId(Long issueFromId, Long issueToId) {
		// TODO Auto-generated method stub
		return issueRelationMapper.findByFromIdAndToId(issueFromId, issueToId);
	}
}